// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const _ = db.command

// 云函数入口函数
exports.main = async (event, context) => {
  try {
    if (event.type == '收入' || event.type == '支出') {
      return await db.collection('user_bill').where({
        _id: event.id
      }).update({
        data: {
          amount: _.set(parseFloat(event.amount)),
          cate: _.set(event.cate),
          account: _.set(event.account),
          datetime: _.set(event.datetime),
          desc: _.set(event.desc)
        },
        success(res) {
          console.log(res)
        }
      })
    }
    else {
      return await db.collection('user_bill').where({
        _id: event.id
      }).update({
        data: {
          amount: _.set(parseFloat(event.amount)),
          account1: _.set(event.account1),
          account2: _.set(event.account2),
          datetime: _.set(event.datetime),
          desc: _.set(event.desc)
        },
        success(res) {
          console.log(res)
        }
      })
    }
  } catch(e) {
    console.log(e)
  }
}